package com.invite.me.beans.presenter;

import android.util.Log;


import com.google.gson.Gson;
import com.invite.me.api.UrlConstants;
import com.invite.me.beans.fabean.MyinfoEdittjFaBean;
import com.invite.me.beans.res.BlindDataParticularsBean;
import com.invite.me.beans.res.MyinfoEdittjBean;
import com.invite.me.beans.res.NameAtteastationtpBean;
import com.invite.me.ui.yaoyue.MyInfoEditActivity;
import com.invite.me.util.SspPrefUtils;
import com.invite.me.wlqq.Constants;
import com.invite.me.wlqq.Contract;
import com.invite.me.wlqq.RxJavaDataImpl;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

public class MyInfoEditPresenter implements Contract.BasePresenter{
    MyInfoEditActivity splashActivity;

    public MyInfoEditPresenter(MyInfoEditActivity signInActivity) {
        this.splashActivity = signInActivity;
        splashActivity.setPresenter(this);
    }
    @Override
    public void start() {

    }


    //图片转换


    //图片转换
    public void shangchuanweijian(File file, String type){
        Map<String,Object> headrMap = new HashMap<>();
        //     headrMap.put("Content-Type","multipart/form-data");
        headrMap.put("userId",SspPrefUtils.getString(Constants.SSPTOKEN, ""));
        //   headrMap.put("userId","78ef0eeb3816f5e9abb77c9fc68e4c1b");
//        CeBeann logeInDataBean = new CeBeann();
//        logeInDataBean.setType(2);
//
//        Gson gson = new Gson();
//        String phoneNumber = gson.toJson(logeInDataBean);
//        RequestBody requestBody = RxJavaDataImpl.getRequestBody(phoneNumber);
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"),file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(),requestFile);
        MultipartBody.Part namebody = MultipartBody.Part.createFormData("type","1");
//        MultipartBody.Part part = MultipartBody.Part.create(requestBody);
//        MultipartBody multipartBody = new MultipartBody.Builder()
//                .setType(MultipartBody.FORM)
//                .addFormDataPart("type", 1)//添加表单数据
//                .build();


        RxJavaDataImpl.uploadFiles(UrlConstants.API_SCTP,headrMap,body,namebody, new Observer() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Object value) {

                if (value instanceof ResponseBody) {
                    try {
                        ResponseBody responseBody = (ResponseBody) value;
                        String str = responseBody.string();
                        Gson gson = new Gson();
                        NameAtteastationtpBean gankInfo = gson.fromJson(str, NameAtteastationtpBean.class);
                        splashActivity.onDataSucess(gankInfo);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }


            @Override
            public void onError(Throwable e) {

                splashActivity.onDataFaile(e.getMessage());
            }

            @Override
            public void onComplete() {
                splashActivity.onComplete();
            }
        });


    }



    //提交实名认证信息
    public void tijiaoshimingrenzheng(String touxiang, String name, int xingbie, List<String> qingkuang, String yaoyueleixing,Double price){

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put(Constants.HEADER_KEY, Constants.HEADER_VALUE);
        hashMap.put("userId", SspPrefUtils.getString(Constants.SSPTOKEN, ""));
        MyinfoEdittjFaBean logeInDataBean = new MyinfoEdittjFaBean();
        logeInDataBean.setDiscribe(touxiang);
        logeInDataBean.setNickname(name);
        logeInDataBean.setSex(xingbie);
        logeInDataBean.setMyTag(qingkuang);
        logeInDataBean.setIntention(yaoyueleixing);
        logeInDataBean.setPrice(price);
        Gson gson = new Gson();
        String phoneNumber = gson.toJson(logeInDataBean);
        RequestBody requestBody = RxJavaDataImpl.getRequestBody(phoneNumber);
        RxJavaDataImpl.postData(UrlConstants.API_TJGRJS, hashMap, requestBody,new Observer() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Object value) {
                if (value instanceof ResponseBody) {
                    try {
                        ResponseBody responseBody = (ResponseBody) value;
                        String str = responseBody.string();
                        Gson gson = new Gson();
                        MyinfoEdittjBean gankInfo = gson.fromJson(str, MyinfoEdittjBean.class);
                        splashActivity.onDataSucess(gankInfo);


                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onError(Throwable e) {
                Log.e("onerror", e+"" );
                splashActivity.onDataFaile(e);
            }

            @Override
            public void onComplete() {
                splashActivity.onComplete();
            }
        });
    }
    //查询个人信息
    public void myxinxi(){

        HashMap<String, Object> hashMap = new HashMap<>();
//       hashMap.put(Constants.HEADER_KEY, Constants.HEADER_VALUE);
        hashMap.put("userId", SspPrefUtils.getString(Constants.SSPTOKEN, ""));
//        MimaDengFaBean logeInDataBean = new MimaDengFaBean();
//        logeInDataBean.setPhone(phone);
//        logeInDataBean.setPassword(mima);
//
//
//        Gson gson = new Gson();
//        String phoneNumber = gson.toJson(logeInDataBean);
//        RequestBody requestBody = RxJavaDataImpl.getRequestBody(phoneNumber);
        RxJavaDataImpl.getData(UrlConstants.API_GRJSXI, hashMap, new Observer() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Object value) {
                if (value instanceof ResponseBody) {
                    try {
                        ResponseBody responseBody = (ResponseBody) value;
                        String str = responseBody.string();
                        Gson gson = new Gson();
                        BlindDataParticularsBean gankInfo = gson.fromJson(str, BlindDataParticularsBean.class);

                        splashActivity.onDataSucess(gankInfo);


                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onError(Throwable e) {
                Log.e("onerror", e+"" );
                splashActivity.onDataFaile(e);
            }

            @Override
            public void onComplete() {
                splashActivity.onComplete();
            }
        });
    }


}
